#include "stdafx.h"
#include "VecCalculator.h"
#include "comment.h"

namespace ftips {

	VecCalculator::VecCalculator(void)
	{
	}

	VecCalculator::~VecCalculator(void)
	{
	}

	// Function: get vector sum
	void VecCalculator::GetVecSum(CvPoint *pVec1, CvPoint *pVec2, CvPoint *pVecSum) {
		pVecSum->x = pVec1->x + pVec2->x;
		pVecSum->y = pVec1->y + pVec2->y; 
	}

	// Function: to calculate cosine value by law of cosines
	double VecCalculator::CalCosineValue(CvPoint *p, CvPoint *pBeforeK, CvPoint *pAfterK) {	
		int dotProduct = (p->x - pBeforeK->x) * (pAfterK->x - p->x)
			+ (p->y - pBeforeK->y) * (pAfterK->y - p->y); // Dot product of two vectors

		double lenBefore = sqrt(pow((double)(p->x - pBeforeK->x), 2)
			+ pow((double)(p->y - pBeforeK->y), 2)); // length of vector1 PPb

		double lenAfter = sqrt(pow((double)(pAfterK->x - p->x), 2)
			+ pow((double)(pAfterK->y - p->y), 2)); // length of vector2 PPa

		return dotProduct / (lenBefore * lenAfter); // Cosine Value
	}

	// Function: to calculate cosine value of two vectors
	double VecCalculator::CalCosineValue(CvPoint *vec1, CvPoint *vec2) {
		int dotProduct = vec1->x * vec2->x + vec1->y * vec2->y; // Dot product of two vectors
		double len1 = sqrt(pow((double)vec1->x, 2) + pow((double)vec1->y, 2)); // length of vector1 PPb
		double len2 = sqrt(pow((double)vec2->x, 2) + pow((double)vec2->y, 2)); // length of vector2 PPa
		return dotProduct / (len1 * len2);
	}

	// Function: grooves point or fingertip by calculating cross product
	bool VecCalculator::CalCrossProduct(CvPoint *p, CvPoint *pBeforeK, CvPoint *pAfterK) {
		return (p->x - pBeforeK->x) * (pAfterK->y - p->y)
			- (p->y - pBeforeK->y) * (pAfterK->x - p->x) < 0 ;
	}

}
